<template>
  <section class="cms-body" v-loading="loading">
    <!-- 返回组件 -->
    <cms-back></cms-back>
    <el-form ref="form" :model="dataInfo" :rules="rules" class="cms-form" label-width="162px">
      <el-form-item label="消息标题" class="flex-50" prop="msgTitle">
        <el-input v-model="dataInfo.msgTitle" class="cms-width"></el-input>
      </el-form-item>
      <el-form-item label="发送状态" class="flex-50" prop="msgStatus">
        <el-radio-group v-model="dataInfo.msgStatus">
          <el-radio :label="true">发布</el-radio>
          <el-radio :label="false">不发布</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="消息内容" class="flex-100 " prop="content">
        <cms-editor :defaultMsg="dataInfo.msgContent" @ready="getUeditor"></cms-editor>
      </el-form-item>
    </el-form>
    <div class="form-footer">
      <el-button type="warning" v-if="isType('save')" @click="add(true)" v-perms="'/message/save'">提交并继续添加
      </el-button>
      <el-button type="primary" v-if="isType('save')" @click="add(false)" v-perms="'/message/save'">添加
      </el-button>
      <el-button type="primary" v-if="isType('update')" @click="update()" v-perms="'/message/update'">修改
      </el-button>
      <el-button type="info" @click="$reset">重置</el-button>
      <span class="gray" v-if="isType('save')">选择“提交并继续添加”按钮会停留在添加页面；选择“提交”按钮后将跳转到对应的列表页</span>
    </div>
  </section>
</template>
<script>
  import axios from "axios";
  import va from "@/rules";
  import formMixns from "@/mixins/form";

  export default {
    mixins: [formMixns],
    data() {
      let required = va.required();
      return {
        ue:[],
        rules: {//校验规则
          msgTitle: [required],
        },
        dataInfo: {
          msgStatus: false,
        }
      };
    },
    methods: {
      getUeditor(ue, index) {
        this.ue[index] = ue; //获取ue实例
      },
      getDataInfo(id, type) {
        if (type == 'update') {
          let api = this.$api; //API地址
          axios.all([
            axios.post(api.messageGet, {msgId: id}),
          ]).then(axios.spread((res) => {
            this.loading = false;
            this.$refs["form"].resetFields();
            this.dataInfo = res.body; //将用户数据复制给dataInfo
          })).catch(error => {
            this.loading = false;
          });
        }
        this.loading = false;
      },
      add(state) {
        this.dataInfo.msgContent = this.ue[0].getContent();
        this.saveDataInfo(state, this.$api.messageSave, this.dataInfo, "pageList");
      },
      update() {
        this.dataInfo.msgContent = this.ue[0].getContent();
        this.updateDataInfo(this.$api.messageUpdate, this.dataInfo, "pageList");
      }
    },
    created() {
      //初始获取数据
      this.getDataInfo(this.id, this.type);
    }
  };
</script>

<style>
  .dialog .tree-layout {
    min-height: 400px;
    width: 90%;
  }

  .dialog .tree-layout .tree {
    width: 100%;
    height: 100%;
    overflow: auto;
    position: absolute;
    border-right: 0px solid #d4dde2;
  }

  .el-dialog__body {
    min-height: 450px;
    overflow: auto;
  }

  .red-color {
    color: red;
  }
</style>
